package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.bpa.DeviceCotDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.*;
import top.infopub.mgr.bpa.domain.DeviceCotDomain;
import top.infopub.mgr.bpa.domain.DeviceLedgerDomain;
import top.infopub.mgr.bpa.service.DeviceCotService;
import top.infopub.mgr.bpa.vo.ContractManageVo;
import top.infopub.mgr.bpa.vo.DeviceCotVo;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.model.bpa.*;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DeviceCotServiceImpl extends GenericServiceImpl<DeviceCot, String> implements
        DeviceCotService {

    @Autowired
    private DeviceCotDao deviceCotDao;
    @Autowired
    private OtherContractsReplenishDao otherContractsReplenishDao;
    @Autowired
    private WsFlowProcessService wsFlowProcess;
    @Autowired
    private OtherContractsReviewDao otherContractsReviewDao;
    @Autowired
    private BpaContractBpaReviewServiceImpl bpaReviewService;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Override
    public GenericDao<DeviceCot, String> getDao() {
        return deviceCotDao;
    }

    @Override
    public Page<DeviceCotDomain> selectPageByDomainWhere(Page<DeviceCotDomain> page,
                                                         DeviceCotVo deviceCotVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(deviceCotVo.getDates())) {
            String[] splitTime = deviceCotVo.getDates().split("~");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            deviceCotVo.setStartDate(format.parse(splitTime[0]));
            deviceCotVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeviceCotDomain>) deviceCotDao.selectByDomainWhere(deviceCotVo);
    }

    @Override
    public Page<DeviceLedgerDomain> selectByLedger(Page<DeviceLedgerDomain> page, ContractManageVo contractManageVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(contractManageVo.getDates())) {
            String[] splitTime = contractManageVo.getDates().split("~");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            contractManageVo.setStartDate(format.parse(splitTime[0]));
            contractManageVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeviceLedgerDomain>) deviceCotDao.selectByLedger(contractManageVo);
    }

    @Override
    public DeviceCotDto queryById(String id) {
        DeviceCotDto dto = new DeviceCotDto();
        //根据id查询主表数据
        DeviceCot deviceCot = deviceCotDao.findById(id);
        //根据主表数据的proCode查询 项目基本信息
        String proCode = deviceCot.getProCode();
        //附件信息
        List<ProjectAttachment> attachmentList = projectAttachmentDao.queryByWhere(deviceCot.getProCode(), ProjectFileTypeEnum.FILE_TYPE95.getCode(), null);
        if (attachmentList.size() > 0 && attachmentList != null) {
            String[] fileAtts = bpaReviewService.fileAttaListToArray(attachmentList);
            dto.setFileAtt(fileAtts);
        }
        if (!StringUtils.isEmpty(proCode)) {
            Basic basic = deviceCotDao.findByProCode(proCode);
            // String projectLeader = otherContractsReview.getProjectLeader();
            //if (!StringUtils.isEmpty(projectLeader)){
            //basic.setProjectLeader(projectLeader);
            //}
            dto.setBasic(basic);
        }
        if (deviceCot != null) {
            dto.setDeviceCot(deviceCot);
        }
        //根据主表数据的id查询 费用清单信息 类型 1合同评审页面  2补充协议页面 3设备合同 4设备合同补充协议
        if (deviceCot != null) {
            List<ExpenseInvoice> expenseInvoiceList = otherContractsReviewDao.selectInvoiceByOtherContractsId(id, "3");
            dto.setExpenseInvoiceList(expenseInvoiceList);
        }
        return dto;
    }

    @Override
    public List<Map<String, String>> projectDropDownBox() {
        return deviceCotDao.projectDropDownBox();
    }

    @Override
    public Basic projectBasic(String proCode) {
        return deviceCotDao.findByProCode(proCode);
    }

    @Override
    public int save(DeviceCotDto dto, List<ProjectAttachment> attachmentList) {
        DeviceCot deviceCot = dto.getDeviceCot();
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        if (deviceCot != null) {
            if (attachmentList != null && attachmentList.size() > 0) {
                //批量插入附件信息
                projectAttachmentDao.deleteByWhere(deviceCot.getProCode(), ProjectFileTypeEnum.FILE_TYPE95.getCode(), null);
                for (ProjectAttachment attachment : attachmentList) {
                    attachment.setProCode(deviceCot.getProCode());
                }
                projectAttachmentDao.insertBatch(attachmentList);
            }
            String id = deviceCot.getId();
            if (StringUtils.isEmpty(id)) {
                //第一次 保存
                deviceCot.setAddBy(vo.getId());
                deviceCot.setAddTime(new Date());
                deviceCot.setProcessStatus("0");
                deviceCot.setId(IdFactory.getUUIDUpper());
                //保存主表数据
                deviceCotDao.insertDeviceCot(deviceCot);
                //保存费用清单信息表 费用清单信息 类型 1合同评审页面  2补充协议页面 3设备合同评审 4设备合同补充协议
                List<ExpenseInvoice> expenseInvoiceList = dto.getExpenseInvoiceList();
                for (ExpenseInvoice expenseInvoice : expenseInvoiceList) {
                    expenseInvoice.setType("3");
                    expenseInvoice.setAddTime(new Date());
                    expenseInvoice.setMasterId(deviceCot.getId());
                    otherContractsReviewDao.insertExpenseInvoice(expenseInvoice);
                }
            } else {
                //修改
                deviceCot.setUptBy(vo.getId());
                deviceCot.setUptTime(new Date());
                deviceCotDao.updateDeviceCot(deviceCot);
                //保存费用清单信息表
                List<ExpenseInvoice> expenseInvoiceList = dto.getExpenseInvoiceList();
                for (ExpenseInvoice expenseInvoice : expenseInvoiceList) {
                    if (StringUtils.isEmpty(expenseInvoice.getId())) {
                        //保存
                        expenseInvoice.setType("3");
                        expenseInvoice.setAddTime(new Date());
                        expenseInvoice.setMasterId(id);
                        otherContractsReviewDao.insertExpenseInvoice(expenseInvoice);
                    } else {
                        //修改
                        expenseInvoice.setType("3");
                        otherContractsReviewDao.updateExpenseInvoice(expenseInvoice);
                    }

                }
            }
        }
        return 1;
    }

    @Override
    public DeviceCot findById(String id) {
        return deviceCotDao.findById(id);
    }

    @Override
    public List<Map<String, String>> projectCompanyBox() {
        return deviceCotDao.projectCompanyBox();
    }

}